package com.ciban.smarthome.common.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DbUtils {
	public static boolean copyDataBase(String db_path, String db_name, InputStream is) {
		boolean result = true;
		File dir = new File(db_path);
		if (!dir.exists()) {
			dir.mkdirs();
		}

		FileOutputStream os = null;
		try {
			File file = new File(db_path, db_name);
			os = new FileOutputStream(file);
			byte[] buffer = new byte[1024];
			int count = 0;
			while ((count = is.read(buffer)) > 0) {
				os.write(buffer, 0, count);
				os.flush();
			}
		} catch (FileNotFoundException e) {
			result = false;
		} catch (IOException e) {
			result = false;
		} finally {
			try {
				if (is != null)
					is.close();
				if (os != null)
					os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

}
